{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典(dict)\n",
    "\n",
    "> 1. 字典的创建方式\n",
    ">> 1. 直接创建字典，按照字典的结构：{key1:value1 , key2:value2 , ......}\n",
    ">> 2. dict()创建字典，dict(key1=value1，key2:value2， ...)\n",
    ">> 3. 新建空字典，然后增加值的方式创建字典\n",
    "> 2. 字典的性质\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第一种创建字典的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'Alex', 'stu10002': 'Jack', 'stu10003': 'Marry'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu10001\":\"Alex\",\n",
    "    \"stu10002\":\"Jack\",\n",
    "    \"stu10003\":\"Marry\"\n",
    "}\n",
    "\n",
    "stu10001 = {\n",
    "    \"name\":\"Alex\",\n",
    "    \"age\":18,\n",
    "    \"Job\":\"student\"\n",
    "}\n",
    "\n",
    "{'stu10001': 'Alex', 'stu10002': 'Jack', 'stu10003': 'Marry'}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第二种创建字典的方法 :dict( )\n",
    "\n",
    "### 与第一种的区别：\n",
    "\n",
    "###     1. dict( ) VS { }\n",
    "\n",
    "###     2. stu10001 VS \"stu10001\"\n",
    "\n",
    "###     3.  =  VS :"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu10001': 'Alex', 'stu10002': 'Jack', 'stu10003': 'Marry'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info_02 = dict(\n",
    "   stu10001 =  \"Alex\",\n",
    "   stu10002 =  \"Jack\",\n",
    "   stu10003 =  \"Marry\",\n",
    ")\n",
    "\n",
    "stu_info_02\n",
    "{'stu10001': 'Alex', 'stu10002': 'Jack', 'stu10003': 'Marry'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第三种创建字典的方式：新建空字典，然后增加值的方式创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info_03 = {}\n",
    "\n",
    "stu_info_03['stu10001'] = \"Alex\" # 字典的增加\n",
    "stu_info_03['stu10002'] = \"Jack\" \n",
    "stu_info_03['stu10003'] = \"Marry\" \n",
    "stu_info_03\n",
    "{'stu10001': 'Alex', 'stu10002': 'Jack', 'stu10003': 'Marry'}\n",
    "stu_info_03['stu10001']\n",
    "'Alex'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (<ipython-input-2-429d9a78c8dc>, line 35)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-429d9a78c8dc>\"\u001b[1;36m, line \u001b[1;32m35\u001b[0m\n\u001b[1;33m    else:\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "\n",
    "## 购物车练习\n",
    " # 商品的数据信息\n",
    "goods_list = [\n",
    "    # [商品名称，商品价格],\n",
    "    ['珍奶',7],\n",
    "    ['大杯珍奶',8.5],\n",
    "    ['加大杯珍奶',9.5],\n",
    "    ['桶装珍奶',20],\n",
    "]\n",
    "user_shopping_car = [\n",
    "    \n",
    "]\n",
    " # 用户输入 使用input,input的使用，结果是一个字符串\n",
    "Alipay = int(input(\"请输入您的余额:\"))\n",
    "\n",
    "while True:\n",
    "     # 循环遍历商品列表，将商品信息按照一定格式输出\n",
    "     # 一定格式：商品编号：商品名称：XX，商品价格：XX\n",
    "    print(\"------请选择编号，欢迎选购------\")\n",
    "    for i in range(len(goods_list)): # range会根据商品列表长度变化而变化\n",
    "        print(\"商品编号:%s,商品名称:%s,商品价格:%s\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "\n",
    "     # 2、让用户输入商品编号进行商品的购买。\n",
    "     # 1.用户输入的内容是否符合规范（用户买的东西有没有）？\n",
    "\n",
    "     # 思考：用户除了输入数值外，有没有可能输入不是数值\n",
    "\n",
    "    choice = input(\"请输入商品编号:\")\n",
    "    if choice.isdigit(): # isdigit 判断字符串里面的内容是否为数值\n",
    "        choice = int(choice)\n",
    "    if choice > len(goods_list):\n",
    "        # b.如果没有该商品，返回没有\n",
    "            print(\"您选择的商品不存在\")\n",
    "                      else:\n",
    "            #  a.如果有 判断金额是否足够:用户金额Alipay 商品的金额 goods_list[choice][1]\n",
    "            if Alipay > goods_list[choice][1]:\n",
    "                # 用户余额够 1.增加该商品到用户购物车 2.扣除余额\n",
    "                user_shopping_car.append(goods_list[choice])\n",
    "                Alipay = Alipay-goods_list[choice][1]\n",
    "                print(\"%s已经加入您的购物车,您的账号余额还有:%s元\"%(goods_list[choice][0],Alipay))\n",
    "            else:\n",
    "                    print(\"您的余额不足，请更换支付方式\")\n",
    "                     elif choice == \"q\":\n",
    "                        # 用户退出购买\n",
    "                        print(\"用户购物车:\",user_shopping_car)\n",
    "                        print(\"您的余额还有:\",Alipay)\n",
    "                        break\n",
    "                        else:\n",
    "            print(\"请输入商品编号！！！\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
